package com.wjggy.core.action.home;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wjggy.core.action.view.PostsView;
import com.wjggy.core.common.page.Page;
import com.wjggy.core.common.page.PageUtil;
import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.entities.Posts;
import com.wjggy.core.service.posts.PostsService;


@Controller
public class HomeAction {
	private Logger logger=Logger.getLogger(HomeAction.class);
	@Autowired
	private PostsService postsService;
	
	@RequestMapping(value="/",method=RequestMethod.GET)
	public ModelAndView index(){
		
		Posts posts=new Posts();
		Page page=new Page();
		
		page.setPageNumber(1);
		page.setPageSize(10);
		//疾病
		posts.setCategoryName("疾病");
		List<Posts> list1=postsService.findPosts(posts, page);
		
		
		
		//保健
		posts.setCategoryName("保健");
		List<Posts> list2=postsService.findPosts(posts, page);
		//互动
		posts.setCategoryName("互动");
		List<Posts> list3=postsService.findPosts(posts, page);
		//疾病
		page.setPageSize(5);
		posts.setCategoryName("疾病");
		List<Posts> list4=postsService.findPosts(posts, page);
		//保健
		posts.setCategoryName("保健");
		List<Posts> list5=postsService.findPosts(posts, page);
		//文化
		posts.setCategoryName("文化");
		List<Posts> list6=postsService.findPosts(posts, page);
		//互动
		posts.setCategoryName("互动");
		List<Posts> list7=postsService.findPosts(posts, page);
		
		ModelAndView modelAndView=new ModelAndView(ViewUtil.style+"index");
		modelAndView.addObject("list1", list1);
		modelAndView.addObject("list2", list2);
		modelAndView.addObject("list3", list3);
		modelAndView.addObject("list4", list4);
		modelAndView.addObject("list5", list5);
		modelAndView.addObject("list6", list6);
		modelAndView.addObject("list7", list7);
		
		return modelAndView;
	}
	@RequestMapping(value="post/{id}",method=RequestMethod.GET)
	public ModelAndView getPost(@PathVariable Long id) {
		Posts posts=new Posts();
		posts.setPostsid(id);
		posts=postsService.findPost(posts);
		ModelAndView modelAndView=new ModelAndView(ViewUtil.style+"post");
		posts.setPosttype(PostsView.POSTS_CATEGORY.get(posts.getCategoryName()));
		Page page2=new Page();
		posts.setForumsid(null);
		page2.setPageNumber(1);
		page2.setPageSize(5);
		//疾病
		posts.setCategoryName("疾病");
		List<Posts> list2=postsService.findPosts(posts, page2);
		
		//保健
		posts.setCategoryName("保健");
		List<Posts> list1=postsService.findPosts(posts, page2);
		//互动
		posts.setCategoryName("互动");
		List<Posts> list4=postsService.findPosts(posts, page2);
		//文化
		posts.setCategoryName("文化");
		List<Posts> list3=postsService.findPosts(posts, page2);
		
		posts.setCategoryName(PostsView.POSTS_CATEGORY.get(posts.getPosttype()));
		modelAndView.addObject("list1", list1);
		modelAndView.addObject("list2", list2);
		modelAndView.addObject("list3", list3);
		modelAndView.addObject("list4", list4);
		
		modelAndView.addObject("posts", posts);
		return modelAndView;
	}
	@RequestMapping(value="post/more/{type}",method=RequestMethod.GET)
	public ModelAndView getPostMore(@PathVariable String type,HttpServletRequest request) {
		
		Posts posts=new Posts();
		Page page= PageUtil.createPage(request,"post/more/"+type,"forumsid");
		page.setPageSize(30);
		//疾病
		posts.setCategoryName(PostsView.POSTS_CATEGORY.get(type));
		page.setResult(postsService.findPosts(posts, page));
		ModelAndView modelAndView=new ModelAndView(ViewUtil.style+"postlist");
		
		Page page2=new Page();
		
		page2.setPageNumber(1);
		page2.setPageSize(5);
		//疾病
		posts.setCategoryName("疾病");
		List<Posts> list2=postsService.findPosts(posts, page2);
		
		//保健
		posts.setCategoryName("保健");
		List<Posts> list1=postsService.findPosts(posts, page2);
		//互动
		posts.setCategoryName("互动");
		List<Posts> list4=postsService.findPosts(posts, page2);
		//文化
		posts.setCategoryName("文化");
		List<Posts> list3=postsService.findPosts(posts, page2);
		posts.setCategoryName(PostsView.POSTS_CATEGORY.get(type));
		modelAndView.addObject("posts", posts);
		modelAndView.addObject("list1", list1);
		modelAndView.addObject("list2", list2);
		modelAndView.addObject("list3", list3);
		modelAndView.addObject("list4", list4);
		modelAndView.addObject("page", page);
		return modelAndView;
	}
	@RequestMapping(value="post/forum/{forumsid}",method=RequestMethod.GET)
	public ModelAndView getPostForums(@PathVariable String forumsid,HttpServletRequest request) {
		
		Posts posts=new Posts();
		Page page= PageUtil.createPage(request,"post/forum/"+forumsid,"forumsid");
		page.setPageSize(30);
		posts.setForumsid(Long.valueOf(forumsid));
		page.setResult(postsService.findPosts(posts, page));
		ModelAndView modelAndView=new ModelAndView(ViewUtil.style+"postlist");
		if (page.getResult().size()>0) {
			Posts post=(Posts) page.getResult().get(0);
			posts.setPosttype(post.getForumName());
		}
		Page page2=new Page();
		posts.setForumsid(null);
		page2.setPageNumber(1);
		page2.setPageSize(5);
		//疾病
		posts.setCategoryName("疾病");
		List<Posts> list2=postsService.findPosts(posts, page2);
		
		//保健
		posts.setCategoryName("保健");
		List<Posts> list1=postsService.findPosts(posts, page2);
		//互动
		posts.setCategoryName("互动");
		List<Posts> list4=postsService.findPosts(posts, page2);
		//文化
		posts.setCategoryName("文化");
		List<Posts> list3=postsService.findPosts(posts, page2);
		posts.setCategoryName(posts.getPosttype());
		modelAndView.addObject("posts", posts);
		modelAndView.addObject("list1", list1);
		modelAndView.addObject("list2", list2);
		modelAndView.addObject("list3", list3);
		modelAndView.addObject("list4", list4);
		modelAndView.addObject("page", page);
		return modelAndView;
	}
}
